cutoff <- as.Date("2012-12-07")

library(tidyverse)
library(cowplot)

load("data/processed/post_id_and_created.RData")

theme_set(theme_bw())


map(c('output/figures/figure_1.eps', 'output/figures/figure_1.png'), 
    ~ ggsave(.x, plot =
               plot_grid(
                 post %>%
                   dplyr::filter(db == 'm5s_blog_feb2016.sqlite' &
                                   posix < cutoff) %>%
                   dplyr::group_by(week = lubridate::floor_date(as.Date(posix), "week")) %>%
                   dplyr::summarise(n_posts = n()) %>%
                   ggplot(aes(x = week, y = n_posts)) +
                   geom_line() +
                   geom_vline(xintercept = cutoff, colour = "gray") +
                   labs(y = "weekly posts", x = "Beppe Grillo's blog\n(beppegrillo.com)") +
                   scale_x_date(limits = c(as.Date("2010-01-01"),
                                           as.Date("2013-01-01"))),
                 post %>%
                   dplyr::filter(db == 'bg_fb_store_jan15.sqlite' &
                                   posix < cutoff) %>%
                   dplyr::group_by(week = lubridate::floor_date(as.Date(posix), "week")) %>%
                   dplyr::summarise(n_posts = n()) %>%
                   ggplot(aes(x = week, y = n_posts)) +
                   geom_line() +
                   geom_vline(xintercept = cutoff, colour = "gray") +
                   labs(y = "weekly posts", x = "Beppe Grillo's page\n(Facebook.com)") +
                   scale_x_date(limits = c(as.Date("2010-01-01"),
                                           as.Date("2013-01-01"))),
                 post %>%
                   dplyr::filter(db == 'forum.sqlite' &
                                   posix < cutoff) %>%
                   dplyr::group_by(week = lubridate::floor_date(as.Date(posix), "week")) %>%
                   dplyr::summarise(n_posts = n()) %>%
                   ggplot(aes(x = week, y = n_posts)) +
                   geom_line() +
                   geom_vline(xintercept = cutoff, colour = "gray") +
                   labs(y = "weekly posts", x = "M5S Forum\n(beppegrillo.it/listeciviche/forum/)") +
                   scale_x_date(limits = c(as.Date("2010-01-01"),
                                           as.Date("2013-01-01"))),
                 post %>%
                   dplyr::filter(db == 'm5s_fb_store_jan15.sqlite' &
                                   posix < cutoff) %>%
                   dplyr::group_by(week = lubridate::floor_date(as.Date(posix), "week")) %>%
                   dplyr::summarise(n_posts = n()) %>%
                   ggplot(aes(x = week, y = n_posts)) +
                   geom_line() +
                   geom_vline(xintercept = cutoff, colour = "gray") +
                   labs(y = "weekly posts", x = "M5S's page\n(Facebook.com)") +
                   scale_x_date(limits = c(as.Date("2010-01-01"),
                                           as.Date("2013-01-01"))),
                 post %>%
                   dplyr::filter(db == 'meetup_event.RData' &
                                   posix < cutoff) %>%
                   dplyr::group_by(week = lubridate::floor_date(as.Date(posix), "week")) %>%
                   dplyr::summarise(n_posts = n()) %>%
                   ggplot(aes(x = week, y = n_posts)) +
                   geom_line() +
                   geom_vline(xintercept = cutoff, colour = "gray") +
                   labs(y = "weekly events", x = "M5S groups\n(Meetup.com)") +
                   scale_x_date(limits = c(as.Date("2010-01-01"),
                                           as.Date("2013-01-01"))),
                 post %>%
                   dplyr::filter(db == 'm5s_public_figures_fb_store_mar15.sqlite' &
                                   posix < cutoff) %>%
                   dplyr::group_by(week = lubridate::floor_date(as.Date(posix), "week")) %>%
                   dplyr::summarise(n_posts = n()) %>%
                   ggplot(aes(x = week, y = n_posts)) +
                   geom_line() +
                   geom_vline(xintercept = cutoff, colour = "gray") +
                   labs(y = "weekly posts", x = "M5S public figures' pages\n(Facebook.com)") +
                   scale_x_date(limits = c(as.Date("2010-01-01"),
                                           as.Date("2013-01-01"))),
                 ncol = 3),
             
             width = 29, height = 14, units = 'cm'
    )
)